package com.ybg.ebs_memory.data;

import com.ybg.ebs_memory.util.Util;

import java.util.Map;

public class Order {
    private int id;
    private String username;
    private long time;
    private int status;
    private Map<Product,Integer> products;
    private float amount;

    public Order(int id,String username, Map<Product, Integer> products) {
        this.id = id;
        this.username = username;
        this.time = System.currentTimeMillis();
        this.status = 0;
        this.products = products;
        for (Map.Entry<Product, Integer> e : products.entrySet()) {
            amount += e.getKey().getPrice() * e.getValue();
        }
    }

    public int getId() {
        return id;
    }

    public String getUsername() {
        return username;
    }

    public long getTime() {
        return time;
    }

    public int getStatus() {
        return status;
    }

    public float getAmount() {
        return amount;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    private String statusInfo(){
        String statusInfo;
        switch (status){
            case 0: statusInfo="未支付"; break;
            case 1: statusInfo="已支付"; break;
            case 2: statusInfo="已退单"; break;
            default: statusInfo="未知"; break;
        }
        return statusInfo;
    }

    public void showLine(String username){
        System.out.println(String.format("%d\t%s\t%s\t%.2f\t%s",
                id,username, Util.dateFormat(time),amount,statusInfo()));
    }

    public void showDetails(String username){
        System.out.println(String.format("订单编号：%d\n用户账号：%s\n下单时间：%s\n订单总额：%.2f（元）\n%s",
                id,username, Util.dateFormat(time),amount,statusInfo()));
        System.out.println("-------------------- 订单详情 --------------------------");
        int id = 1;
        for (Map.Entry<Product, Integer> e : products.entrySet()) {
            Product product = e.getKey();
            Integer count = e.getValue();
            System.out.println(String.format("%d\t%s\t%.2f\t%d\t%.2f",
                    id++,product.getName(),product.getPrice(),count,product.getPrice()*count));
        }
        System.out.println("--------------------------------------------------------");
    }
}
